package com.ruifu.common.utils;

import com.fhzncloud.cloud.admin.api.entity.SysUserDept;
import com.fhzncloud.cloud.admin.api.feign.RemoteDeptService;
import com.fhzncloud.cloud.common.core.constant.SecurityConstants;
import com.fhzncloud.cloud.common.core.util.R;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.core.toolkit.CollectionUtils.*;
import static org.springframework.util.CollectionUtils.containsAny;

/**
 * 权限函数计算
 * @author lcq
 * @create 2020/4/15
 */
@Slf4j
@Component
public class PermissionsUtlis {
    /**
     * 用户;部门;角色 <>相关函数计算</>
     * "165,168;200,204;104;"UserInfoFeign
     * @return
     */
    public RemoteDeptService remoteDeptService;


    public Boolean permissions(String var) {
        //获取当前用户信息
        CloudUser user = SecurityUtils.getUser();
        log.info("用户"+user.getUsername()+"当前用户id-->："+user.getId());
        log.info("用户"+user.getUsername()+"当前部门id-->："+user.getDeptId());
        //获取当前用户角色集合 Integer集合转String集合
        List<String> roleStrings = SecurityUtils.getRoles().stream().map(Object::toString).collect(Collectors.toList());
        log.info("用户"+user.getUsername()+"当前用户角色id-->："+StringUtils.join(roleStrings, ","));

        //指定用户id集合
        List<String> designatedUserIdList = new ArrayList<>();
        //指定部门id集合
        List<String> designatedDeptIdList = new ArrayList<>();
        //指定角色id集合
        List<String> designatedRoleIdList = new ArrayList<>();

        String[] strings = var.split(";");
        for (int i = 0; i < strings.length; i++) {
            switch (i){
                case 0:
                    //指定用户集合
                    designatedUserIdList = new ArrayList<>(Arrays.asList(strings[i].split(",")));
                    log.info("规则中指定用户集合-->："+StringUtils.join(designatedUserIdList, ","));
                    break;
                case 1:
                    //指定部门集合
                    designatedDeptIdList = new ArrayList<>(Arrays.asList(strings[i].split(",")));
                    log.info("规则中部门集合-->："+StringUtils.join(designatedDeptIdList, ","));
                    break;
                case 2:
                    //指定角色集合
                    designatedRoleIdList = new ArrayList<>(Arrays.asList(strings[i].split(",")));
                    log.info("规则中指定角色集合-->："+StringUtils.join(designatedRoleIdList, ","));
                    break;
                default:break;
            }
        }
        //部门信息集合
        List<SysUserDept> userDeptList=new ArrayList<>();
        //判断指定部门是否为空
      if (!"".equals(StringUtils.join(designatedDeptIdList, ","))){
          for (String designatedDeptId : designatedDeptIdList) {
              //指定部门信息对象
              SysUserDept designatedUserDept = new SysUserDept();
              designatedUserDept.setDeptId(Integer.valueOf(designatedDeptId));
              designatedUserDept.setUserId(user.getId());
              designatedUserDept.setTenantId(user.getTenantId());
              userDeptList.add(designatedUserDept);
          }
      }
        //部门信息
        List<Integer> deptList=new ArrayList<>();
      //判断部门信息集合是否为空
        if (isNotEmpty(userDeptList)){
          R allChildDept = remoteDeptService.getAllChildDept(userDeptList, SecurityConstants.FROM_IN);
            log.info("Feign调用：获取所有子部门");
          if (allChildDept!=null){
              deptList = (List<Integer>) allChildDept.getData();
              log.info("指定部门id-->："+StringUtils.join(deptList, ","));
          }
        }
        boolean isRoleIntersection= containsAny(roleStrings, designatedRoleIdList);
        log.info("角色是否有交集："+isRoleIntersection);

        Integer deptId = user.getDeptId();
        boolean isDeptIntersection= deptList.contains(deptId);
        log.info("部门是否有包含："+isDeptIntersection);

        Integer userId = user.getId();
        boolean isUserIntersection=(designatedUserIdList.contains(userId+""));
        log.info("用户是否有包含："+isUserIntersection);

        if (isRoleIntersection || isDeptIntersection || isUserIntersection){
            return true;
        }
        return false;
    }
}
